import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

import com.google.inject.Guice;
import com.google.inject.Injector;
import com.sosv.breweryDB.connector.entity.Style;
import com.sosv.breweryDB.connector.entity.beer.Beer;
import com.sosv.breweryDB.connector.entity.brewery.Brewery;
import com.sosv.breweryDB.connector.guice.BreweryDBAsyncModule;
import com.sosv.breweryDB.connector.service.IBreweryDBService;
import com.sosv.breweryDB.connector.service.async.IBreweryDBServiceAsync;
import com.sosv.breweryDB.connector.service.async.IResultCallback;
import com.sosv.breweryDB.connector.service.exceptions.ApiKeyNotFoundExeption;
import com.sosv.breweryDB.connector.service.resource.filter.beer.BeersFilter;


public class Tester {
	
	
	public static void main(String [] args) throws ApiKeyNotFoundExeption, IOException{
		System.out.println("start");

		// Create the injector, the async module creates the sync and async object
		Injector injector = Guice.createInjector(new BreweryDBAsyncModule());
		
		// Get the sync service
		final IBreweryDBService syncService = injector
		        .getInstance(IBreweryDBService.class);
		// Do some sync calls
		//Beer result = syncService.getBeerById("0kkUpk");
		//System.out.println("Sync: " + result);
		//Get beers by name
		List<Beer> beers = syncService.getAllBeers();
		//List<Brewery> breweries = syncService.getAllBreweries();
		//List<Style> styles = syncService.getAllStyles();
		FileWriter outstream = new FileWriter("C:\\Users\\Joshua\\Desktop\\beers_new.csv");
		BufferedWriter out = new BufferedWriter(outstream);
//		out.write("Id,Name,Desc,Category,\n");
//		for (Style style : styles)
//		{
//			try {
//				out.write(""+style.getId());
//				out.write(",");
//				out.write(style.getName().replace(",", ""));
//				out.write(",");
//				out.write(style.getDescription().replace(",", ""));
//				out.write(",");
//				out.write(""+style.getCategory().getName());
//				out.write(",\n");
//				
//			}
//			
//			catch(Exception e)
//			{
//				out.write("\n");
//				continue;
//			}
//			
//			out.flush();
//		}
//		for (Brewery brewery : breweries)
//		{
//			try {
//				out.write(brewery.getId());
//				out.write(",");
//				out.write(brewery.getName().replace(",", ""));
//				out.write(",\n");
//			}
//			
//			catch(Exception e)
//			{
//				out.write("\n");
//				continue;
//			}
//			
//			out.flush();
//		}
//		
		out.write("Id,Name,CreationDate,Organic,Status,StatusDisplay,AvailableId,GlassId,StyleId,Abv,Breweries,IBU,\n");
		for (Beer beer : beers)
		{
			try {
				out.write(beer.getId());
				out.write(",");
				out.write(beer.getName());
				out.write(",");
				out.write(beer.getCreateDate());
				out.write(",");
				out.write(beer.getIsOrganic());
				out.write(",");
				out.write(beer.getStatus());
				out.write(",");
				out.write(beer.getStatusDisplay());
				out.write(",");
				out.write(""+beer.getAvailableId());
				out.write(",");
				out.write(""+beer.getGlasswareId());
				out.write(",");
				out.write(""+beer.getStyleId());
				out.write(",");
				out.write(beer.getAbv());
				out.write(",");
				for (Brewery brewery : beer.getBreweries())
				{
				  out.write(brewery.getId());
				  out.write(";");
				}	
				out.write(",");
				out.write(beer.getIbu());
				out.write(",");
				out.write("\n");
				out.flush();
			}
			catch (Exception e){
				out.write("\n");
				out.flush();
				continue;
			}
		}
		out.close();

		// Get the async service
//		final IBreweryDBServiceAsync asyncService = injector
//		        .getInstance(IBreweryDBServiceAsync.class);
//
//		// Do some async calls
//		asyncService.getAllBeers(new IResultCallback<List<Beer>>() {
//
//		    public void onSuccess(List<Beer> result) {
//		        System.out.println(result);
//		    }
//
//		    public void onError(Throwable throwable) {
//		        throwable.printStackTrace();
//		    }
//		});
//
//		asyncService.getBeerById("cBLTUw", new IResultCallback<Beer>() {
//
//		    public void onSuccess(Beer result) {
//		        System.out.println("Async:" + result);
//		    }
//
//		    public void onError(Throwable throwable) {
//		        throwable.printStackTrace();
//		    }
//		});

		System.out.println("Done");
	}
}
